package com.marvelution.servicemix.samples.calculator;

import javax.jws.WebService;

@WebService(serviceName = "CalculatorService", targetNamespace = "http://www.marvelution.com/servicemix/samples/calculator", endpointInterface = "com.marvelution.servicemix.samples.calculator.CalculatorPortType")
public class CalculatorImpl implements CalculatorPortType {

	public CalculateResponse calculate(CalculateRequest calculateRequest) throws UnknownOperatorFault_Exception {
		String operator = calculateRequest.getOperator();
		if (operator == null || "".equalsIgnoreCase(operator)) {
			UnknownOperatorFault fault = new UnknownOperatorFault();
			fault.setMessage("You must supply a operator. Supported operators are: +, -, *, /, %");
			throw new UnknownOperatorFault_Exception(null, fault);
		}
		double result = 0;
		if ("+".equals(operator)) {
			result = calculateRequest.getLeftnumber() + calculateRequest.getRightnumber();
		} else if ("-".equals(operator)) {
			result = calculateRequest.getLeftnumber() - calculateRequest.getRightnumber();
		} else if ("*".equals(operator)) {
			result = calculateRequest.getLeftnumber() * calculateRequest.getRightnumber();
		} else if ("/".equals(operator)) {
			result = calculateRequest.getLeftnumber() / calculateRequest.getRightnumber();
		} else if ("%".equals(operator)) {
			result = calculateRequest.getLeftnumber() % calculateRequest.getRightnumber();
		} else {
			UnknownOperatorFault fault = new UnknownOperatorFault();
			fault.setMessage("Unsupported operator found. Supported operators are: +, -, *, /, %");
			throw new UnknownOperatorFault_Exception(null, fault);
		}
		
		CalculateResponse response = new CalculateResponse();
		response.setOperation(calculateRequest.getLeftnumber() + " " + calculateRequest.getOperator() + " " + calculateRequest.getRightnumber());
		response.setResult(result);
		return response;
	}

}
